import {pageModalTip as pagetip, filterIp, filterPort} from '/js/usemain.js';
import {previewJson} from '/nginxConfig/js/previewdata.js'
import {default as BeanNginxService} from '/nginxConfig/js/bean/BeanNginxService.js'

//==========================================================================
//
//==========================================================================
let initflag = true;

let dataarea = $('#section4content1').children('table').children('tbody');
let thispagerow = 10;

let modifyclientservice = new BeanNginxService();
let modifyline = -1;
//==========================================================================
//
//==========================================================================
function clientserviceInitFillTag() {
	let html='';
	for (let i = 0; i < thispagerow; i++) {
		html += '<tr>'
			+ '<td class="d-none">'
			+  '<span>id0</span>'
			+  '<span>id1</span>'
			+  '<span>id2</span>'
			+  '<span>id3</span>'
			+ '</td>'
			+ '<td>'
			+  '<div></div>'
			+ '</td>'
			+ '<td>'
			+  '<div></div>'
			+ '</td>'
			+ '<td>'
			+  '<div></div>'
			+ '</td>'
			+ '<td>'
			+  '<div></div>'
			+ '</td>'
			+ '<td>'
			+  '<div></div>'
			+ '</td>'
			+ '<td>'
			+  '<div></div>'
			+ '</td>'
			+ '<td>'
			+  '<div></div>'
			+ '</td>'
			+ '<td class="font-weight-bold text-primary text-center">'
			+ '</td>'
			+ '<td>'
			+  '<span class="small btn btn-danger btn-sm ml-2 edit-clientservice">修改</span>'
			+  '<span class="small btn btn-danger btn-sm ml-2 delete-clientservice">删除</span>'
			+ '</td>'
			+ '</tr>';
	}
	html += '<tr class="table-success" id="addclientservice">'
		+ '<td class="d-none"></td>'
		+ '<td>'
		+  '<input type="text" class="form-control" placeholder="">'
		+ '</td>'
		+ '<td>'
		+  '<select class="getMyIpList form-control"></select>'
		+ '</td>'
		+ '<td>'
		+  '<input type="text" class="form-control ipcheck" placeholder="80">'
		+ '</td>'
		+ '<td>'
		+  '<select class="getAllIpList form-control"></select>'
		+ '</td>'
		+ '<td>'
		+  '<input type="text" class="form-control ipcheck" placeholder="8080">'
		+ '</td>'
		+ '<td>'
		+  '<select class="getAllIpList form-control"></select>'
		+ '</td>'
		+ '<td>'
		+  '<input type="text" class="form-control ipcheck" placeholder="8080">'
		+ '</td>'
		+ '<td>'
		+  '<input type="checkbox" class="form-control"/>'
		+ '</td>'
		+ '<td class="btn-group btn-group-sm">'
		+  '<button type="button" class="small btn btn-primary btn-sm ml-1 save-add-clientservice">保存</button>'
		+  '<button type="button" class="small btn btn-info btn-sm ml-1 cancel-add-clientservice">取消</button>'
		+ '</td>'
		+ '</tr>';
	dataarea.html(html);
	dataarea.children().hide();
}
function clientserviceFillData(data) {
	let total = 0;
	if (data) {
		total = data.length > thispagerow ? thispagerow : data.length;
		$.each(eval(data), function(i, one) {
			if (i < total) {
				dataarea.children('tr:eq('+i+')').show();
				dataarea.children('tr:eq('+i+')').children('td:eq(0)').children('span:eq(0)').html(one.sid);
				dataarea.children('tr:eq('+i+')').children('td:eq(0)').children('span:eq(1)').html(one.sListenId);
				dataarea.children('tr:eq('+i+')').children('td:eq(0)').children('span:eq(2)').html(one.fontEndId);
				dataarea.children('tr:eq('+i+')').children('td:eq(0)').children('span:eq(3)').html(one.backEndId);
				
				dataarea.children('tr:eq('+i+')').children('td:eq(1)').children('div').html(one.sName);
				dataarea.children('tr:eq('+i+')').children('td:eq(2)').children('div').html(one.sListenIp);
				dataarea.children('tr:eq('+i+')').children('td:eq(3)').children('div').html(one.sListenPort);
				dataarea.children('tr:eq('+i+')').children('td:eq(4)').children('div').html(one.fontEndIp);
				dataarea.children('tr:eq('+i+')').children('td:eq(5)').children('div').html(one.fontEndPort);
				dataarea.children('tr:eq('+i+')').children('td:eq(6)').children('div').html(one.backEndIp);
				dataarea.children('tr:eq('+i+')').children('td:eq(7)').children('div').html(one.backEndPort);
				dataarea.children('tr:eq('+i+')').children('td:eq(8)').html(('true'==one.allowXlsx?'√':'×'));
				dataarea.children('tr:eq('+i+')').show();
			}
		});
	}
}

function clientserviceClearDataOfRow(i) {
	if(i >= 0) {
		dataarea.children('tr:eq('+i+')').children('td:eq(0)').children('span:eq(0)').html('');
		dataarea.children('tr:eq('+i+')').children('td:eq(0)').children('span:eq(1)').html('');
		dataarea.children('tr:eq('+i+')').children('td:eq(0)').children('span:eq(2)').html('');
		dataarea.children('tr:eq('+i+')').children('td:eq(0)').children('span:eq(3)').html('');
		
		dataarea.children('tr:eq('+i+')').children('td:eq(1)').children('div').html('');
		dataarea.children('tr:eq('+i+')').children('td:eq(2)').children('div').html('');
		dataarea.children('tr:eq('+i+')').children('td:eq(3)').children('div').html('');
		dataarea.children('tr:eq('+i+')').children('td:eq(4)').children('div').html('');
		dataarea.children('tr:eq('+i+')').children('td:eq(5)').children('div').html('');
		dataarea.children('tr:eq('+i+')').children('td:eq(6)').children('div').html('');
		dataarea.children('tr:eq('+i+')').children('td:eq(7)').children('div').html('');
		dataarea.children('tr:eq('+i+')').children('td:eq(8)').html('');
		dataarea.children('tr:eq('+i+')').hide();
	}
}

function clientserviceClearAddTag() {
	$('#addclientservice').children('td:eq(1)').children('input').val(null);
	$('#addclientservice').children('td:eq(2)').children('select').val(null);
	$('#addclientservice').children('td:eq(3)').children('input').val(null);
	$('#addclientservice').children('td:eq(4)').children('select').val(null);
	$('#addclientservice').children('td:eq(5)').children('input').val(null);
	$('#addclientservice').children('td:eq(6)').children('select').val(null);
	$('#addclientservice').children('td:eq(7)').children('input').val(null);
	$('#addclientservice').children('td:eq(8)').children('input').prop('checked',false);
	$('#addclientservice').hide();
}
function clientserviceRowRecover(){
	$('#clientservice').children('div:eq(0)').children('div:eq(1)').show();
	$('.delete-clientservice').show();
	$('.edit-clientservice').show();
	let i = modifyline;
	modifyline = -1;
	if (i>=0) {
		let rowhtml = dataarea.children('tr:eq('+i+')');
		rowhtml.children('td:eq(9)').children('span:eq(3)').remove();
		rowhtml.children('td:eq(9)').children('span:eq(2)').remove();
		rowhtml.children('td:eq(1)').html('<div>'+modifyclientservice.sName+'</div>');
		rowhtml.children('td:eq(2)').html('<div>'+modifyclientservice.sListenIp+'</div>');
		rowhtml.children('td:eq(3)').html('<div>'+modifyclientservice.sListenPort+'</div>');
		rowhtml.children('td:eq(4)').html('<div>'+modifyclientservice.sFontEndIp+'</div>');
		rowhtml.children('td:eq(5)').html('<div>'+modifyclientservice.sFontEndPort+'</div>');
		rowhtml.children('td:eq(6)').html('<div>'+modifyclientservice.sBackEndIp+'</div>');
		rowhtml.children('td:eq(7)').html('<div>'+modifyclientservice.sBackEndPort+'</div>');
		rowhtml.children('td:eq(8)').html((modifyclientservice.sSupportXlsx?'√':'×'));
	}
}

function clientserviceRowToEdit(){
	modifyclientservice.msg();
	
	let i = modifyline;
	$('#section4content1').children('label').children('span:eq(1)').css("opacity",0.5);// 表外部的新增按钮
	// 表区域
	$('.delete-clientservice').hide();
	$('.edit-clientservice').hide();
	if (i>=0) {
		let rowhtml = dataarea.children('tr:eq('+i+')');
		rowhtml.children('td:eq(1)').html('<input type="text" class="form-control" value="'+modifyclientservice.sName+'">');
		rowhtml.children('td:eq(2)').html('<select class="getMyIpList form-control"></select>');
		rowhtml.children('td:eq(3)').html('<input type="text" class="form-control" value="'+modifyclientservice.sListenPort+'">');
		rowhtml.children('td:eq(4)').html('<select class="getAllIpList form-control"></select>');
		rowhtml.children('td:eq(5)').html('<input type="text" class="form-control" value="'+modifyclientservice.sFontEndPort+'">');
		rowhtml.children('td:eq(6)').html('<select class="getAllIpList form-control"></select>');
		rowhtml.children('td:eq(7)').html('<input type="text" class="form-control" value="'+modifyclientservice.sBackEndPort+'">');
		rowhtml.children('td:eq(8)').html('<input type="radio" class="form-control"/>');
		if (modifyclientservice.sSupportXlsx) {
			rowhtml.children('td:eq(8)').children('input[type="radio"]').prop('checked',true);
		}
		rowhtml.children('td:eq(9)').append('<span class="small btn btn-primary btn-sm ml-1 save-add-clientservice">保存</span>'
				+  '<span type="button" class="small btn btn-info btn-sm ml-1 cancel-add-clientservice">取消</span>');
		
		rowhtml.children('td:eq(9)').children('span:eq(2)').click(clientserviceEdit);
		rowhtml.children('td:eq(9)').children('span:eq(3)').click(clientserviceCancelEdit);
		rowhtml.children('td:eq(2)').children('select').focus(getMyIpList);
		rowhtml.children('td:eq(4)').children('select').focus(getAllIpList);
		rowhtml.children('td:eq(6)').children('select').focus(getAllIpList);
	}
}

function getMyIpList() {
	clientserviceAskMyIpList($(this).parent().children('select'));
}
function getAllIpList() {
	clientserviceAskAllIpList($(this).parent().children('select'));
}

//==========================================================================
//前端向后端的请求
//==========================================================================

function clientserviceAskSearch(){
	$.get('../nginxserver/getServiceList', 
			{'typeBs':false},	
			function(result) {
				if (result.status>=0) {
					if (result.data) {
						clientserviceFillData(result.data);
					}
				} else {
					if (result.tips && result.tips.length > 0) {
						pagetip('获取服务配置信息列表', result.tips);
					} else {
						pagetip('获取服务配置信息列表', '获取列表信息失败');
					}
				}
		});
}
function clientserviceAskDel(id){
	if (id && parseInt(id) > 0) {
		$.get('../nginxserver/delService', 
			{'typeBs':false,
			"id" : id}, 
			function(result) {
				if (result.status>=0) {
					if (result.data) {
						alert('删除成功');
						clientserviceSearch();
					} else {
						alert('删除失败');
					}
				} else {
					if (result.tips && result.tips.length > 0) {
						alert(result.tips);
					} else {
						alert('删除失败');
					}
				}
			});
	}
}

function clientserviceAskSave(pagetiphead, saveNginxService){
	$.get('../nginxserver/saveService', {
		"typeBs":false,
		"sid" : saveNginxService.sId,
		"sName" : saveNginxService.sName,
		"sListenId" : saveNginxService.sListenId,
		"sListenPort" : saveNginxService.sListenPort,
		"fontEndId" : saveNginxService.sFontEndId,
		"fontEndPort" : saveNginxService.sFontEndPort,
		"backEndId" : saveNginxService.sBackEndId,
		"backEndPort" : saveNginxService.sBackEndPort,
		"sSendXlsx":saveNginxService.sSupportXlsx
	}, function(result) {
		if (result.status>=0) {
			if (result.data) {
				pagetip(pagetiphead,'保存成功');
				clientserviceSearch();
			} else {
				pagetip(pagetiphead,'保存失败');
			}
		} else {
			if (result.tips && result.tips.length > 0) {
				pagetip(pagetiphead,result.tips);
			} else {
				pagetip(pagetiphead,'保存失败');
			}
		}
	});
}

function clientserviceAskMyIpList(area) {
	$.get('../nginxserver/getLocalIpList', function(result) {
		if (result.status>=0) {
			if (result.data) {
				let html = '';
				$.each(eval(result.data), function(i, one) {
					html += '<option value="' + one.id + '">' + one.ip + '</option>';
				});
				$(area).html(html);
			}
		} else {
			if (result.tips && result.tips.length > 0) {
				pagetip('获取本机IP（IPv4）列表', result.tips);
			} else {
				pagetip('获取本机IP（IPv4）列表', '获取IP列表失败');
			}
		}
	});
}
function clientserviceAskAllIpList(area) {
	$.get('../nginxserver/getAllIpList', function(result) {
		if (result.status>=0) {
			if (result.data) {
				let html = '';
				$.each(eval(result.data), function(i, one) {
					html += '<option value="' + one.id + '">' + one.ip + '</option>';
				});
				$(area).html(html);
			}
		} else {
			if (result.tips && result.tips.length > 0) {
				pagetip('获取其他IP（IPv4）列表', result.tips);
			} else {
				pagetip('获取其他IP（IPv4）列表', '获取IP列表失败');
			}
		}
	});
}
//==========================================================================
//界面按钮的事件
//==========================================================================

function clientserviceSearch(){
	for (let i = 0; i < thispagerow; i++) {
		clientserviceClearDataOfRow(i);
	}
	if (1 != $('#section4content1').children('label').children('span:eq(1)').css("opacity")) {
		$('#section4content1').children('label').children('span:eq(1)').css("opacity",1);
	}
	clientserviceRowRecover();
	clientserviceClearAddTag();
	clientserviceAskSearch();
}
function clientserviceDelete(){
	let rowtr = $(this).parent().parent();
//	let index = rowtr.index();
	let id = rowtr.children('td:eq(0)').children('span:eq(0)').html();
	clientserviceAskDel(id);
}
function clientserviceBeforeAdd(){
	if (1 == $('#section4content1').children('label').children('span:eq(1)').css("opacity")) {
		$('#addclientservice').show();
		$('.delete-clientservice').hide();
		$('.edit-clientservice').hide();
		$('#section4content1').children('label').children('span:eq(1)').css("opacity",0.5);
	}
}
function clientserviceAdd() {
	let tiphead = '新增服务配置相关信息';
	let sname =  $('#addclientservice').children('td:eq(1)').children('input').val();
	if (null == sname || 0 == sname.trim().length) {
		pagetip(tiphead,'请设置服务名称');
		return;
	}
	sname = sname.trim();
	let slistenid = parseInt($('#addclientservice').children('td:eq(2)').children('select').val());
	if (!Number.isInteger(slistenid)) {
		pagetip(tiphead,'未选择监听IP');
		return;
	}
	let slistenport = filterPort($('#addclientservice').children('td:eq(3)').children('input').val());
	if (!slistenport) {
		pagetip(tiphead,'未设置合法的监听端口号');
		return;
	}
	let sfontendid = parseInt($('#addclientservice').children('td:eq(4)').children('select').val());
	if (!Number.isInteger(sfontendid)) {
		pagetip(tiphead,'未选择前端IP');
		return;
	}
	let sfontendport = filterPort($('#addclientservice').children('td:eq(5)').children('input').val());
	if (!sfontendport) {
		pagetip(tiphead,'未设置合法的前端端口号');
		return;
	}
	let sbackendid = parseInt($('#addclientservice').children('td:eq(6)').children('select').val());
	if (!Number.isInteger(sbackendid)) {
		pagetip(tiphead,'未选择后端IP');
		return;
	}
	let sbackendport = filterPort($('#addclientservice').children('td:eq(7)').children('input').val());
	if (!sbackendport) {
		pagetip(tiphead,'未设置合法的后端端口号');
		return;
	}
	let saveNginxService = new BeanNginxService();
	saveNginxService.sName = sname;
	saveNginxService.sListenId = slistenid;
	saveNginxService.sListenPort = slistenport;
	saveNginxService.sFontEndId = sfontendid;
	saveNginxService.sFontEndPort = sfontendport;
	saveNginxService.sBackEndId = sbackendid;
	saveNginxService.sBackEndPort = sbackendport;
	if ($('#addclientservice').children('td:eq(8)').children('input[type="checkbox"]').is(":checked")) {
		saveNginxService.sSupportXlsx = true;
	}
	clientserviceAskSave(tiphead, saveNginxService);
}

function clientserviceCancelAdd(){
	clientserviceClearAddTag();
	$('#section4content1').children('label').children('span:eq(1)').css("opacity",1);
	$('.delete-clientservice').show();
	$('.edit-clientservice').show();
}

function clientserviceBeforeEdit(){
	modifyclientservice.clear();
	let rowhtml = $(this).parent().parent();
	modifyline = rowhtml.index();
	modifyclientservice.sId = parseInt(rowhtml.children('td:eq(0)').children('span:eq(0)').html().trim());
	modifyclientservice.sListenId = parseInt(rowhtml.children('td:eq(0)').children('span:eq(1)').html().trim());
	modifyclientservice.sFontEndId = parseInt(rowhtml.children('td:eq(0)').children('span:eq(2)').html().trim());
	modifyclientservice.sBackEndId = parseInt(rowhtml.children('td:eq(0)').children('span:eq(3)').html().trim());
	
	modifyclientservice.sName = rowhtml.children('td:eq(1)').children('div:eq(0)').html();
	modifyclientservice.sListenIp = rowhtml.children('td:eq(2)').children('div:eq(0)').html();
	modifyclientservice.sListenPort = rowhtml.children('td:eq(3)').children('div:eq(0)').html();
	modifyclientservice.sFontEndIp = rowhtml.children('td:eq(4)').children('div:eq(0)').html();
	modifyclientservice.sFontEndPort = rowhtml.children('td:eq(5)').children('div:eq(0)').html();
	modifyclientservice.sBackEndIp = rowhtml.children('td:eq(6)').children('div:eq(0)').html();
	modifyclientservice.sBackEndPort = rowhtml.children('td:eq(7)').children('div:eq(0)').html();
	
	if ('√' == rowhtml.children('td:eq(8)').html()) {
		modifyclientservice.sSupportXlsx = true;
	} else {
		modifyclientservice.sSupportXlsx = false;
	}
	clientserviceRowToEdit();
}

function clientserviceEdit(){
	if (null!=modifyclientservice && $.isNumeric(modifyclientservice.sId) && modifyclientservice.sId > 0) {
		let rowhtml = $(this).parent().parent();
		
		let unchanged = true;
		let tiphead = '修改服务配置相关信息';
		let sname =  rowhtml.children('td:eq(1)').children('input').val();
		if (null == sname || 0 == sname.trim().length) {
			pagetip(tiphead,'服务名称不得为空');
			return;
		}
		sname = sname.trim();
		let slistenid = parseInt(rowhtml.children('td:eq(2)').children('select').val());
		let slistenport = filterPort(rowhtml.children('td:eq(3)').children('input').val());
		if (!slistenport) {
			pagetip(tiphead,'监听端口号不合法');
			return;
		}
		let sfontendid = parseInt(rowhtml.children('td:eq(4)').children('select').val());
		let sfontendport = filterPort(rowhtml.children('td:eq(5)').children('input').val());
		if (!sfontendport) {
			pagetip(tiphead,'前端端口号不合法');
			return;
		}
		let sbackendid = parseInt(rowhtml.children('td:eq(6)').children('select').val());
		let sbackendport = filterPort(rowhtml.children('td:eq(7)').children('input').val());
		if (!sbackendport) {
			pagetip(tiphead,'后端端口号不合法');
			return;
		}
		let sendxlsx = rowhtml.children('td:eq(8)').children('input').is(":checked");
		
		let saveNginxService = new BeanNginxService();
		$.extend(saveNginxService, modifyclientservice);
		if (Number.isInteger(slistenid)) {
			if (slistenid != saveNginxService.sListenId) {
				saveNginxService.sListenId = slistenid;
				if (unchanged) {
					unchanged = false;
				}
			}
		}
		if (Number.isInteger(sfontendid)) {
			if (saveNginxService.sFontEndId != sfontendid) {
				saveNginxService.sFontEndId = sfontendid;
				if (unchanged) {
					unchanged = false;
				}
			}
		}
		if (Number.isInteger(sbackendid)) {
			if (saveNginxService.sBackEndId != sbackendid) {
				saveNginxService.sBackEndId = sbackendid;
				if (unchanged) {
					unchanged = false;
				}
			}
		}
		if (saveNginxService.sName != sname) {
			saveNginxService.sName = sname;
			if (unchanged) {
				unchanged = false;
			}
		}
		if (saveNginxService.sListenPort != slistenport) {
			saveNginxService.sListenPort = slistenport;
			if (unchanged) {
				unchanged = false;
			}
		}
		if (saveNginxService.sFontEndPort != sfontendport) {
			saveNginxService.sFontEndPort = sfontendport;
			if (unchanged) {
				unchanged = false;
			}
		}
		if (saveNginxService.sBackEndPort != sbackendport) {
			saveNginxService.sBackEndPort = sbackendport;
			if (unchanged) {
				unchanged = false;
			}
		}
		if (saveNginxService.sSupportXlsx !=sendxlsx) {
			saveNginxService.sSupportXlsx =sendxlsx;
			if (unchanged) {
				unchanged = false;
			}
		}
		if (unchanged) {
			pagetip(tiphead,'没有要修改的信息');
		} else {
			clientserviceAskSave(tiphead, saveNginxService);
		}
	}
}

function clientserviceCancelEdit(){
	clientserviceRowRecover();
	$('#section4content1').children('label').children('span:eq(1)').css("opacity",1);
	modifyclientservice.clear();
}
//==========================================================================
//初始化执行操作
//==========================================================================
function init() {
	clientserviceInitFillTag();
	$('.getMyIpList').focus(getMyIpList);
	$('.getAllIpList').focus(getAllIpList);
	$('.edit-clientservice').click(clientserviceBeforeEdit);
	$('.delete-clientservice').click(clientserviceDelete);
	$('#addclientservice').children('td:eq(9)').children('button:eq(0)').click(clientserviceAdd);
	$('#addclientservice').children('td:eq(9)').children('button:eq(1)').click(clientserviceCancelAdd);
	initflag = false;
	$('#section4content1').children('label:eq(0)').children('span:eq(0)').click(clientserviceSearch);
	$('#section4content1').children('label:eq(0)').children('span:eq(1)').click(clientserviceBeforeAdd);
}

export default init;

